package com.five.welcome.utils;

import org.springframework.stereotype.Component;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.Base64;

/**
 * @author Ywt
 * MultipartFile转成Base64
 */
@Component
public class MultipartFileToBase64Util {

    public String generateBase64(MultipartFile file) {
        if (file == null || file.isEmpty()) {
            throw new RuntimeException("图片不能为空！");
        }
        String base64EncoderImg = "";
        try {
            byte[] fileBytes = file.getBytes();
            base64EncoderImg = Base64.getEncoder().encodeToString(fileBytes);
        } catch (IOException e) {
            e.printStackTrace();
        }
        return base64EncoderImg;
    }
}
